﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace SocketAPI
{
    public class messageReceiver
    {
        public Socket socket;
        public IPEndPoint localEndPoint;
        public ListView lvLog;
        public Label txtStatus;
        public socketAPI.SocketServerConn.addLogCallback addLog;
        public socketAPI.SocketServerConn.setStatusCallback setStatus;
        public string mainMsgString = "";
        public messageReceiver(Socket socket, IPEndPoint localEndPoint, socketAPI.SocketServerConn.addLogCallback addLog, socketAPI.SocketServerConn.setStatusCallback setStatus)
        {
            this.setStatus = setStatus;
            this.addLog = addLog;
            this.socket = socket;
            this.localEndPoint = localEndPoint;
        }
        public void runThread()
        {
            try
            {
                IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
                String ipStr = "";
                foreach (IPAddress address in hostEntry.AddressList)
                {
                   string addr = address.ToString();
                    if (addr.Contains(":") == false)
                    {
                        ipStr = addr;
                    }
                }
                mainMsgString = ipStr + ":" + localEndPoint.Port.ToString();
                socket.Connect(localEndPoint);

                setStatus("Connected!");
                
                addLog("Socket connected to " +
                    socket.RemoteEndPoint.ToString());

                sendMessage("TYPE-CONTROL:PASS-12345", socket);
                
                while (true)
                {
                    byte[] bytes = new byte[1024];
                    // Receive the response from the remote device.
                    int bytesRec = socket.Receive(bytes);
                    addLog("Received: " + Encoding.ASCII.GetString(bytes, 0, bytesRec));
                    parseData(Encoding.ASCII.GetString(bytes));
                }
                // Release the socket.
                socket.Close();

                socket.Shutdown(SocketShutdown.Both);
            }
            catch (ArgumentNullException ane)
            {
                addLog("ArgumentNullException : " + ane.ToString());
                setStatus("Disconnected!");
            }
            catch (SocketException se)
            {
                addLog("SocketException : " + se.ToString());
                setStatus("Disconnected!");
            }
            catch (Exception e)
            {
                addLog("Unexpected exception : " + e.ToString());
                setStatus("Disconnected!");
            }
        }
        public void parseData(string data)
        {
            string temp = "";
            if (data.StartsWith("*") == true)
            {
                //Client Data Message
                temp = data.Replace("*CLIENT*>", "");
                string[] dataSplit = Regex.Split(temp, ":");
                string address = dataSplit[0];
                string port = dataSplit[1];
                
            }
        }
        public void sendMessage(string message, Socket handler)
        {
            byte[] msgData = Encoding.ASCII.GetBytes(mainMsgString + ":" + message);
            handler.Send(msgData);
        }
    }
}
